//
//  UITableViewController+HDChangeGender.m
//  app
//
//  Created by D on 15/4/14.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDChangeGender.h"
#import "HDUserInforHttpRequestManger.h"
//#import "HZForumSQLiteManager.h"

@implementation HDChangeGender

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改性别";
    
    
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(save:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"gender"] integerValue] == 1) {
        self.genderLab.text = @"男";
        [self.genderSelect setSelectedSegmentIndex:1];
    }else
    {
        self.genderLab.text = @"女";
        [self.genderSelect setSelectedSegmentIndex:0];
    }

    self.result_gender = [JKRemoteResult target:self action:@selector(changeGender:)];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)selectGender:(id)sender {
    if ([self.genderSelect selectedSegmentIndex] == 0) {
        self.genderLab.text = @"女";
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"gender"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else
    {
        self.genderLab.text = @"男";
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"gender"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
-(void)save:(UIButton*)sender
{
    self.progress_ = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.progress_];
    [self.view bringSubviewToFront:self.progress_];
    self.progress_.delegate = self;
    [self.progress_ show:YES];
    
    HDUserInforHttpRequestManger * httpManager = [HDUserInforHttpRequestManger shareUserInforHttpRequestManger];
    NSDictionary * params = @{@"gender":[[NSUserDefaults standardUserDefaults]objectForKey:@"gender"]};
    
    [httpManager changeUserInfoWithParams:params reuslt:self.result_gender];
    
}
-(void)changeGender:(JKRemoteResult*)rt
{
    if (self.progress_) {
        [self.progress_ removeFromSuperview];
        self.progress_ = nil;
    }
    
    if (rt.failure) {
        UIAlertView * av = [[UIAlertView alloc]initWithTitle:nil message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:av];
        [av show];
        
        return;
    }
    NSLog(@"msg = %@",rt.response.message);
    if (rt.response.status == 0) {
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"edit_gender"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        //    删除生活圈缓存
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [alertView removeFromSuperview];
}

@end
